//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Core
{
    public class ExtendedProperties : IOfficeXmlSerializable
    {
        private string _template;
        private string _manager;
        private string _Company;
        private int? _Pages;
        private int? _Words;
        private int? _Characters;
        private string _PresentationFormat;
        private int? _Lines;
        private int? _Paragraphs;
        private int? _Slides;
        private int? _Notes;
        private int? _TotalTime;
        private int? _HiddenSlides;
        private int? _MMClips;
        private bool? _ScaleCrop;
        private bool? _LinksUpToDate;
        private int? _CharactersWithSpaces;
        private bool? _SharedDoc;
        private bool? _HyperlinksChanged;
        private string _HyperlinkBase;
        private string _Application;
        private string _AppVersion;
        private int? _DocSecurity;
        private Vector _TitlesOfParts;
        private Vector _HeadingPairs;
        private Vector _HLinks;
        private string _digSig;

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read("Properties"))
            {
                while (c.ReaderLocalName != null)
                {
                    switch (c.ReaderLocalName)
                    {
                        case "Template":
                            _template = c.ReadElementString("Template");
                            break;
                        case "Manager":
                            _manager = c.ReadElementString("Manager");
                            break;
                        case "Company":
                            _Company = c.ReadElementString("Company");
                            break;
                        case "Pages":
                            _Pages = int.Parse(c.ReadElementString("Pages"));
                            break;
                        case "Words":
                            _Words = int.Parse(c.ReadElementString("Words"));
                            break;
                        case "Characters":
                            _Characters = int.Parse(c.ReadElementString("Characters"));
                            break;
                        case "PresentationFormat":
                            _PresentationFormat = c.ReadElementString("PresentationFormat");
                            break;
                        case "Lines":
                            _Lines = int.Parse(c.ReadElementString("Lines"));
                            break;
                        case "Paragraphs":
                            _Paragraphs = int.Parse(c.ReadElementString("Paragraphs"));
                            break;
                        case "Slides":
                            _Slides = int.Parse(c.ReadElementString("Slides"));
                            break;
                        case "Notes":
                            _Notes = int.Parse(c.ReadElementString("Notes"));
                            break;
                        case "TotalTime":
                            _TotalTime = int.Parse(c.ReadElementString("TotalTime"));
                            break;
                        case "HiddenSlides":
                            _HiddenSlides = int.Parse(c.ReadElementString("HiddenSlides"));
                            break;
                        case "MMClips":
                            _MMClips = int.Parse(c.ReadElementString("MMClips"));
                            break;
                        case "ScaleCrop":
                            _ScaleCrop = bool.Parse(c.ReadElementString("ScaleCrop"));
                            break;
                        case "HeadingPairs":
                            using (var cHeadingPairs = c.Read("HeadingPairs"))
                            {
                                _HeadingPairs = cHeadingPairs.ReadElement<Vector>();
                            }
                            break;
                        case "TitlesOfParts":
                            using (var cTitlesOfParts = c.Read("TitlesOfParts"))
                            {
                                _TitlesOfParts = cTitlesOfParts.ReadElement<Vector>();
                            }
                            break;
                        case "LinksUpToDate":
                            _LinksUpToDate =bool.Parse( c.ReadElementString("LinksUpToDate"));
                            break;
                        case "CharactersWithSpaces":
                            _CharactersWithSpaces = int.Parse(c.ReadElementString("CharactersWithSpaces"));
                            break;
                        case "SharedDoc":
                            _SharedDoc = bool.Parse(c.ReadElementString("SharedDoc"));
                            break;
                        case "HyperlinkBase":
                            _HyperlinkBase = c.ReadElementString("HyperlinkBase");
                            break;
                        case "HLinks":
                            using (var cHLinks = c.Read("HLinks"))
                            {
                                _HLinks = cHLinks.ReadElement<Vector>();
                            }
                            break;
                        case "HyperlinksChanged":
                            _HyperlinksChanged = bool.Parse(c.ReadElementString("HyperlinksChanged"));
                            break;
                        case "DigSig":
                            _digSig = c.ReadElementString("DigSig");
                            break;
                        case "Application":
                            _Application = c.ReadElementString("Application");
                            break;
                        case "AppVersion":
                            _AppVersion = c.ReadElementString("AppVersion");
                            break;
                        case "DocSecurity":
                            _DocSecurity = int.Parse(c.ReadElementString("DocSecurity"));
                            break;
                        default:
                            throw new NotSupportedException();
                    }
                }
            }

        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write("Properties"))
            {
                c.DeclareNamespace(OfficeConstants.VariantNamespace);
                c.WriteOptionalElementString("Application", _Application);
                if (_DocSecurity != null)
                    c.WriteOptionalElementString("DocSecurity", _DocSecurity.ToString());
                if (_ScaleCrop != null)
                    c.WriteOptionalElementString("ScaleCrop", Variant.GetXmlBooleanString(_ScaleCrop.Value));

                if (_HeadingPairs != null)
                {
                    using (var cHeadingPairs = c.Write("HeadingPairs"))
                    {
                        cHeadingPairs.WriteOptionalElement(_HeadingPairs);
                    }

                }


                if (_TitlesOfParts != null)
                {
                    using (var cTitlesOfParts = c.Write("TitlesOfParts"))
                    {
                        cTitlesOfParts.WriteOptionalElement(_TitlesOfParts);
                    }

                }
                c.WriteOptionalElementString("Company", _Company);
                if (_LinksUpToDate != null)
                    c.WriteOptionalElementString("LinksUpToDate", Variant.GetXmlBooleanString(_LinksUpToDate.Value));
                if (_SharedDoc != null)
                    c.WriteOptionalElementString("SharedDoc", Variant.GetXmlBooleanString(_SharedDoc.Value));

                if (_HyperlinksChanged != null)
                    c.WriteOptionalElementString("HyperlinksChanged", Variant.GetXmlBooleanString(_HyperlinksChanged.Value));

                c.WriteOptionalElementString("AppVersion", _AppVersion);

                
                c.WriteOptionalElementString("Template", _template);
                c.WriteOptionalElementString("Manager", _manager);
                if (_Pages != null)c.WriteOptionalElementString("Pages", _Pages.ToString());



                if (_Words != null)
                    c.WriteOptionalElementString("Words", _Words.ToString());
                if (_Characters != null)
                c.WriteOptionalElementString("Characters", _Characters.ToString());
                c.WriteOptionalElementString("PresentationFormat", _PresentationFormat);
                if (_Lines != null)
                    c.WriteOptionalElementString("Lines", _Lines.ToString());
                if (_Paragraphs != null)
                    c.WriteOptionalElementString("Paragraphs", _Paragraphs.ToString());
                if (_Slides != null)
                    c.WriteOptionalElementString("Slides", _Slides.ToString());
                if (_Notes != null)
                    c.WriteOptionalElementString("Notes", _Notes.ToString());
                if (_TotalTime != null)
                    c.WriteOptionalElementString("TotalTime", _TotalTime.ToString());
                if (_HiddenSlides != null)
                    c.WriteOptionalElementString("HiddenSlides", _HiddenSlides.ToString());
                if (_MMClips != null)
                    c.WriteOptionalElementString("MMClips", _MMClips.ToString());
                if (_CharactersWithSpaces != null)
                    c.WriteOptionalElementString("CharactersWithSpaces", _CharactersWithSpaces.ToString());
                c.WriteOptionalElementString("HyperlinkBase", _HyperlinkBase);
                if (_HLinks != null)
                {
                    using (var cHLinks = c.Write("HLinks"))
                    {
                        cHLinks.WriteOptionalElement(_HLinks);
                    }
                    
                }
                c.WriteOptionalElementString("DigSig", _digSig);
            }
        }
    }
}